<template>
  <el-drawer
      v-model="isShow"
      size="50%"
      :destroy-on-close="true"
      @close="closeHandle"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :show-close="false"
  >
  <template #header="{ close, titleId, titleClass }">
          <h4 :id="titleId" :class="titleClass">{{ title }}</h4>
      </template>
  <el-form :model="form"  ref="formRef" :rules="rules" v-loading="loading">
      <el-row class="mb-4" :gutter="20">
        <el-col :span="24">
          <el-form-item label="发生地点" required prop="roomId" label-width="110px">
            <el-input v-model="form.roomId" :rows="3" :readonly="isreadonly" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="发生时间" required prop="breakrolesdate" label-width="110px">
                <el-date-picker
                v-model="form.breakrolesdate"
                type="datetime"
                placeholder="请选择发生时间"
                value-format="YYYY-MM-DD HH:mm:ss"
                style="width:100%"
                :readonly="isreadonly"
            />
            </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="违规违纪情况"  required prop="detail" label-width="110px">
                <el-input v-model="form.detail" type="textarea" :rows="3" :readonly="isreadonly" />
            </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="处置情况"  required prop="treatment" label-width="110px">
              <el-select
                v-model="form.treatment"
                placeholder="请选择处置情况"
                :disabled="isreadonly"
              >
                <el-option
                    v-for="item in nj_weigui_dispose"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
            </el-select>
            </el-form-item>
        </el-col>
      </el-row>
  </el-form>
  <template #footer>
      <el-button type="primary" v-if="!isreadonly" @click="Confirm">确认</el-button>
      <el-button @click="isShow = false">关闭</el-button>
  </template>
  </el-drawer>
</template>
<script setup>

import {getCurrentInstance} from "vue";
const {proxy} = getCurrentInstance()
import {BreakRolesAdd,BreakRolesInfo,BreakRolesEdit} from "@/api/ApplyDetail/ApplyDetail";
import { getUserProfile } from "@/api/system/user";
import {roomStateList} from "@/api/BAApply/BAApply";

const {nj_weigui_dispose,nj_sheanren_type} = proxy.useDict( 'nj_weigui_dispose','nj_sheanren_type')

const isShow = ref(false)
const loading = ref(false)
const isreadonly = ref(false)

const RoomqueryParams = ref({unitId:null,policeAreaType:1})
const Roompaging = ref({pageNum: 1,pageSize: 99})

const RoomList = ref([])

const title = ref('新增违规违纪登记')
const form = ref({
})
const changeShowStatus = () => {
  isShow.value = !isShow.value
}
const rules = {
  roomId: [{ required: true, message: "发生地点不能为空", trigger: "blur" }],
  breakrolesdate: [{ required: true, message: "发生时间不能为空", trigger: "blur" }],
  detail: [{ required: true, message: "违规违纪情况不能为空", trigger: "blur" }],
  treatment: [{ required: true, message: "处置情况不能为空", trigger: "blur" }],
}
// 通知事件
const emit = defineEmits(['getList'])
onMounted(() => {
  
})
// 获取个人信息
const getUserInfo = async () => {
    await getUserProfile().then(res => {
        RoomqueryParams.value.unitId = res.data.unitId
        getroomStateLists()
    })
}
// 获取房间列表
const getroomStateLists = async () => {
    await roomStateList(RoomqueryParams.value, Roompaging.value).then(res => {
      // 使用 reduce 合并所有的 baseRoomList
      RoomList.value = res.data.reduce((acc, item) => {
        return acc.concat(item.baseRoomList);
      }, []);
    })
}
// 获取详情
const getInfo = async (jwApplydetailId) => {
  await BreakRolesInfo(jwApplydetailId).then(res => {
    form.value = res.data
    loading.value = false
  })
}
const showAdd = async (jwApplydetailId,data) => {
  changeShowStatus()
  getUserInfo()
  console.log("show新增",jwApplydetailId,data)
  form.value.jwApplydetailId = jwApplydetailId
}

const showEdit = async (data,isSee) => {
  changeShowStatus()
  if(isSee == 'See'){
      title.value = '违规违纪详情'
      isreadonly.value = true
      loading.value = true
      getInfo(data.jw_breakroles_id)
  }else{
    title.value = '违规违纪编辑'
      isreadonly.value = false
      loading.value = true
      getInfo(data.jw_breakroles_id)
  }
}
// 确认按钮
const Confirm = async () => {
  proxy.$refs["formRef"].validate(valid => {
  if (valid) {
      if(form.value['jwBreakrolesId']){
          loading.value = true
          BreakRolesEdit(form.value).then(res => {
              loading.value = false
              changeShowStatus()
              emit('getList', '来了老弟')
          })
      } else {
        BreakRolesAdd(form.value).then(res => {
              loading.value = false
              changeShowStatus()
              proxy.$modal.msgSuccess(`新增成功`)
              emit('getList', '来了老弟')
          })
      }
  }
});
}

defineExpose({showAdd,showEdit})

// 关闭后的回调事件
const closeHandle = () => {
  form.value = {}
  emit('getList', '来了来了')
  console.log(123123)
}
</script>
<style>

</style>